import { defineStore } from 'pinia'
import { routes } from '@/router/routes'
import router from '@/router'
import { removeToken } from '@/utils/token-utils'

// 创建仓库
const useUserStore = defineStore('userInfo', {
  state: () => {
    return {
      token: localStorage.getItem('token') || '',
      userInfo: JSON.parse(localStorage.getItem('userInfo') as any) || '',
      TabBarRoutes: [...routes] //存储路由
    }
  },

  actions: {
    // 登录成功存储token
    addToken(token: string) {
      this.token = token
    },

    // 获取用户信息
    async addUserInfo(userInfo: any) {
      this.userInfo = userInfo
    },

    async getUserInfo() {
      const res = await reqGetUserInfo()
      if (res.code === 200) {
        // 获取用户信息成功
        this.userInfo = res.data
      } else {
        // router('/login')
        return Promise.reject('error')
      }
    },
    // 退出登录
    logOut() {
      removeToken()
      this.token = ''
    }
  },

  getters: {}
})

export default useUserStore
function reqGetUserInfo() {
  throw new Error('Function not implemented.')
}
